/* eslint-disable indent */
import Vue from 'vue'
import {
    hasAccess,
    clearNoNum
} from '@/utils/index'
Vue.mixin({
    data () {
        return {
            seachData: {},
            supplierSourch: undefined
        }
    },
    methods: {
        turnToPage (route) {
            let { name, params, query, meta } = {}
            if (typeof route === 'string') name = route
            else {
                name = route.name
                params = route.params
                query = route.query
                meta = route.meta
            }
            if (name.indexOf('isTurnByHref_') > -1) {
                let { href } = this.$router.resolve({ name: name.split('_')[1], query: query })
                window.open(href, '_blank')
                return
            }
            this.$router.push({
                name,
                params,
                query,
                meta
            })
        },
        handleTabClick (type) {
            this.type = type
            this.$refs.proTable.resetForm()
            this.$router.push(this.$route.path + '?type=' + type)
            this.getTableData(this.seachData)
        },
        handleSizeChange (pageSize) {
            this.pageConfig.attr['page-size'] = pageSize
            this.pageConfig.attr['current-page'] = 1
            if (JSON.stringify(this.seachData) !== '{}') {
                this.getTableData(this.seachData)
            } else {
                this.getTableData()
            }
        },
        handleCurrentChange (currentPage) {
            this.pageConfig.attr['current-page'] = currentPage
            if (JSON.stringify(this.seachData) !== '{}') {
                this.getTableData(this.seachData)
            } else {
                this.getTableData()
            }
        },
        setSubmitData (data) {
            for (const key in this.formConfig.form.model) {
                if (this.formConfig.form.model[key] !== '') {
                    if (this.formConfig.form.model[key] instanceof Array && this.formConfig.form.model[key].length) {
                        if (key.includes('Time')) {
                            data[key + '.min'] = this.formConfig.form.model[key][0]
                            data[key + '.max'] = this.formConfig.form.model[key][1]
                        } else {
                            data[key] = this.formConfig.form.model[key].join(',')
                        }
                    } else if (this.formConfig.form.model[key] !== null) {
                        data[key] = this.formConfig.form.model[key]
                    }
                }
            }
            return data
        },
        submitForm (data) {
            this.seachData = {}
            this.seachData = data
            // console.log(data, 'data')
            this.getTableData(this.seachData)
        },
        searchBtnClick (type) {
            let data = this.formConfig.form.model
            this.tableConfig.tableMethods.toggleSelection()
            if (type === 'reset') {
                this.$refs.proTable.resetForm()
            }
            this.submitForm(data)
        },
        getSourceFrom (sourceFrom) {
            if (sourceFrom === 'own' || !sourceFrom) {
                sourceFrom = 1001
                this.formConfig.form.model.free = ''
            } else if (sourceFrom === 'supplier') {
                sourceFrom = 1005
                this.formConfig.form.model.free = ''
            } else if (sourceFrom === 'jd') {
                sourceFrom = 1002
                this.formConfig.form.model.free = ''
            } else if (sourceFrom === 'yanxuan') {
                sourceFrom = 1003
                this.formConfig.form.model.free = true
            } else if (sourceFrom === 'thirdPartyGoods') {
                sourceFrom = '1006,1007'
                this.formConfig.form.model.free = ''
            }
            if (this.supplierSourch === 'supplier') {
                sourceFrom = 1005
            }

            if (sourceFrom === 'tmall') {
                sourceFrom = 1010
            }
            if (sourceFrom === 'xinfutong') {
                sourceFrom = 1008
            }
            if (sourceFrom === 'staples') {
                sourceFrom = 1004
            }
            return sourceFrom
        },
        viewAccess (access) {
            if (this.$route.meta.access) {
                return hasAccess(access, this.$route)
            } else {
                return false
            }
        },
        // 过滤状态
        filterStatus (status) {
            switch (status) {
                case 'sellOut':
                case '审核通过':
                    if (!this.showGrid) {
                        status = '在售'
                    } else {
                        status = '上架'
                    }
                    break
                case '上架':
                case '在售':
                case 'sell':
                    status = '停售'
                    break
                case '停售':
                    status = '在售'
                    break
                case 'readySell':
                case '待审核':
                    status = '审核'
                    break
                case '审核不通过':
                    status = '已审核'
                    break
                default:
                    break
            }
            return status
        },
        // 过滤更新方式
        filterUpdateWay (item) {
            return (item.goodsUpdateType && item.goodsUpdateType.description) || (item.updateType && item.updateType.description)
        },
        // 表格排序
        handleSort (value) {
            if (this.$refs.proTable) {
                let sortValue = this.$refs.proTable.handleSort(value)
                this.formConfig.form.model.sort = sortValue
                this.submitForm(this.formConfig.form.model)
            }
        },
        formatPrice (val, val2, val3) {
            val = val.toString()
            return clearNoNum(val, val2, false)
        },
        filterNum (val) {
            return val.replace(/\D/g, '')
        }
    }
})
